{{/* Show site search? */}}
{{ $show_search := site.Params.header.navbar.show_search | default false }}
{{ if $show_search }}
{{ $pf_css := resources.Get "dist/pagefind/pagefind-ui.css" }}
{{ $pf_css = $pf_css | resources.Fingerprint "sha256" }}
<link type="text/css" rel="stylesheet" href="{{ $pf_css.RelPermalink }}" integrity="{{ $pf_css.Data.Integrity }}" />
{{ $pf_js := resources.Get "dist/pagefind/pagefind-ui.js" }}
{{ $pf_js = $pf_js | resources.Fingerprint "sha256" }}
<script src="{{ $pf_js.RelPermalink }}" integrity="{{ $pf_js.Data.Integrity }}"></script>

<style>
  html.dark {
    --pagefind-ui-primary: #eeeeee;
    --pagefind-ui-text: #eeeeee;
    --pagefind-ui-background: #152028;
    --pagefind-ui-border: #152028;
    --pagefind-ui-tag: #152028;
  }
</style>

<script>
  window.addEventListener('DOMContentLoaded', (event) => {
    new PagefindUI({ element: "#search", showSubResults: true });
  });
  document.addEventListener('DOMContentLoaded', () => {
    let element = document.getElementById('search');
    let trigger = document.getElementById('search_toggle');

    if (trigger) {
      trigger.addEventListener('click', () => {
        element.classList.toggle('hidden');
        element.querySelector("input").value = ""
        element.querySelector("input").focus()

        if (!element.classList.contains('hidden')) {
          let clear_trigger = document.querySelector('.pagefind-ui__search-clear');

          if (clear_trigger && !clear_trigger.hasAttribute('listenerOnClick')) {
            clear_trigger.setAttribute('listenerOnClick', 'true');

            clear_trigger.addEventListener('click', () => {
              element.classList.toggle('hidden');
            });
          }
        }

      });
    }
  });
</script>
{{ end }}

{{/* Mermaid */}}
{{ if (.Page.Store.Get "has_mermaid") }}
  {{ $mermaid_js := resources.Get "dist/lib/mermaid/mermaid.min.js" }}
  {{ $mermaid_config_js := resources.Get "js/hb-mermaid-config.js" }}
  {{ $mermaid_config_js = $mermaid_config_js | resources.Minify }}
  {{ $mermaid_bundle := slice $mermaid_js $mermaid_config_js | resources.Concat "js/mermaid.bundle.js" | resources.Fingerprint "sha256" }}
  <script defer src="{{ $mermaid_bundle.RelPermalink }}" integrity="{{ $mermaid_bundle.Data.Integrity }}"></script>
{{ end }}

{{/* Markmap */}}
{{ if (.Page.Store.Get "has_markmap") }}
<style>
  .markmap > svg {
    width: 100%;
    height: 100%;
  }
</style>
<script>
  window.markmap = {
    autoLoader: {
      manual: false,
      onReady() {
        const { autoLoader, builtInPlugins } = window.markmap;
        // Disable Prism highlighting plugin as it conflicts and attempts to re-render code blocks outside of Markmap.
        autoLoader.transformPlugins = builtInPlugins.filter(plugin => plugin.name !== 'prism');
      },
    },
  };
</script>
{{ $markmap_js := resources.Get "dist/lib/markmap/index.js" }}
{{ $markmap_js = $markmap_js | resources.Minify | resources.Fingerprint "sha256" }}
<script defer src="{{ $markmap_js.RelPermalink }}" integrity="{{ $markmap_js.Data.Integrity }}"></script>
{{ end }}

{{/* Katex */}}
{{ if (.Page.HasShortcode "math") | or .Params.math | or site.Params.features.math.enable }}
  {{ $katex_css := resources.Get "dist/lib/katex/katex.min.css" }}
  {{ $katex_css = $katex_css | resources.Fingerprint "sha256" }}
  <link type="text/css" rel="stylesheet" href="{{ $katex_css.RelPermalink }}" integrity="{{ $katex_css.Data.Integrity }}" />
  {{ $katex_js := resources.Get "dist/lib/katex/katex.min.js" }}
  {{ $katex_js = $katex_js | resources.Fingerprint "sha256" }}
  <script defer src="{{ $katex_js.RelPermalink }}" integrity="{{ $katex_js.Data.Integrity }}"></script>
  {{ $katex_render_js := resources.Get "dist/lib/katex/auto-render.min.js" }}
  {{ $katex_config_js := resources.Get "js/katex-config.js" }}
  {{ $katex_config_js = $katex_config_js | resources.Minify }}
  {{ $katex_bundle := slice $katex_render_js $katex_config_js | resources.Concat "js/katex-renderer.js" | resources.Fingerprint "sha256" }}
  <script defer src="{{ $katex_bundle.RelPermalink }}" integrity="{{ $katex_bundle.Data.Integrity }}"></script>
  {{ $katex_fonts := resources.Match "dist/lib/katex/fonts/*" }}
  {{ range $katex_fonts }}
    {{ .Publish }}
  {{ end }}
{{ end }}

{{/* Plotly */}}
{{ if .Page.HasShortcode "chart" }}
{{ $plotly_js := resources.Get "dist/lib/plotly/plotly.min.js" }}
{{ $plotly_js = $plotly_js | resources.Fingerprint "sha256" }}
<script defer src="{{ $plotly_js.RelPermalink }}" integrity="{{ $plotly_js.Data.Integrity }}"></script>
{{ end }}
